package it.asvp.asvp_portal.web.controller;

import it.asvp.asvp_portal.asvp_util.util.Constants;
import it.asvp.asvp_portal.asvp_util.util.DateUtils;
import it.asvp.asvp_portal.data.entity.art.Art;
import it.asvp.asvp_portal.data.entity.art.ArtPicture;
import it.asvp.asvp_portal.data.entity.domain.File;
import it.asvp.asvp_portal.service.services.ArtService;
import it.asvp.asvp_portal.web.bean.ArtPictureBean;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/art")
public class ArtController extends BaseController{

	private static final Log log = LogFactory.getLog(ArtController.class);
	@Autowired
	ArtService artService;
	
	@RequestMapping(value = "/default", method = RequestMethod.GET)
	public ModelAndView getAll(final HttpServletRequest request, HttpServletResponse response) {
    
		log.info("Accesso alla pagina di tutti gli oggetti d'arte");
		
		try{
			
			ModelAndView mv = getModelAndView("art/default");
		    mv.addObject(Constants.Art.ARTS, artService.getAllArts());
		    return mv;
		    
	    }catch (Exception e) {
		   return getErrorModelAndView(e);
	    }
	}
	
	@RequestMapping(value = "/insert", method = RequestMethod.GET)
	public ModelAndView insert(@ModelAttribute("ArtPictureBean") ArtPictureBean artPictureBean) {

		log.info("Accesso alla form di inserimento di un nuovo art");
		
	    try{	
	    	
	       ModelAndView mv = getModelAndView("art/insert");
		   mv.addObject("allArtType", artPictureBean.getAllArtType());
		   return mv;	
		   
	    }catch (Exception e) {
		   return getErrorModelAndView(e);
	    }
	}
	
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
    public ModelAndView save(@ModelAttribute("ArtPictureBean") ArtPictureBean artPictureBean) throws Exception{
		
        //log
		try{
			
            Art art=artPictureBean.getArt();
            //validazione
            List<MultipartFile> files=artPictureBean.getFiles();
            Set<ArtPicture> artPictureSet=new HashSet<ArtPicture>();
			
			if(null != files && files.size() > 0) {
	            for (MultipartFile multipartFile : files) {
	            	
	            	if(multipartFile!=null || !multipartFile.isEmpty()){
	                     ArtPicture artPicture=new ArtPicture();
	                     File file=new File();
	                     artPicture.setArt(art); 
	                     file.setName(multipartFile.getOriginalFilename());
	                     file.setData(multipartFile.getBytes());
	                     artPicture.setArt(art);
	                     artPicture.setFile(file);
	                     artPictureSet.add(artPicture);
	            	}
	            }
	        }
		
            if(artPictureSet.size()>0){
                    art.setArtPictures(artPictureSet);
            }
            
            artService.addArt(getUserDetail(), art);
         
            return new ModelAndView(Constants.REDIRECT+"/art/default");
            
       }catch (Exception e) {
		   return getErrorModelAndView(e);
	   }
    }
	
	@RequestMapping(value = "/delete/{idArt}", method = RequestMethod.POST)
	public ModelAndView delete(Long idArt) throws Exception{
		
		//log
		return null;
	}
	
	@RequestMapping(value = "/edit/{idArt}", method = RequestMethod.GET)
	public ModelAndView edit(Long idArt) throws Exception{
		
		//log
		return null;
	}
	
	@RequestMapping(value = "/update/{idArt}", method = RequestMethod.POST)
	public ModelAndView update(Long idArt) throws Exception{
		
		//log
		return null;
	}
}
